<script setup>
import { ref, provide, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute()
const router = useRouter()
import { useSetUpStore } from '@/stores'

const setUpStore = useSetUpStore()

const checkTimeRange = (lightCurrentTime, darkCurrentTime, isChange) => {
  const startHour = parseInt(lightCurrentTime[0], 10)
  const startMinute = parseInt(lightCurrentTime[1], 10)
  const endHour = parseInt(darkCurrentTime[0], 10)
  const endMinute = parseInt(darkCurrentTime[1], 10)
  if (
    startHour > endHour ||
    (startHour === endHour && startMinute > endMinute)
  ) {
    checkTimeRange(darkCurrentTime, lightCurrentTime, 1)
    return
  }

  const now = new Date()
  const currentHour = now.getHours()
  const currentMinute = now.getMinutes()

  if (
    (currentHour > startHour ||
      (currentHour === startHour && currentMinute >= startMinute)) &&
    (currentHour < endHour ||
      (currentHour === endHour && currentMinute < endMinute))
  ) {
    console.log(isChange ? 'dark' : 'light')

    return isChange ? 'dark' : 'light'
  } else {
    console.log(isChange ? 'light' : 'dark')

    return isChange ? 'light' : 'dark'
  }
}
// 主题
const theme = computed(() => {
  // let index = setUpStore.getThemeIndex()
  // if (index === 1) {
  //   return 'light'
  // } else if (index === 2) {
  //   return 'dark'
  // }
  switch (setUpStore.getThemeIndex()) {
    case 1:
      return 'light'
    case 2:
      return 'dark'
    case 3:
      return checkTimeRange(setUpStore.getLightTime(), setUpStore.getDarkTime())
    default:
      return 'light'
  }
})
// 音乐播放器组件
const musicPlayerRef = ref(null)
// 显示音乐播放器
const showMusicPlayer = () => {
  // console.log(route.path)
  musicPlayerRef.value.showMusicPlayer()
  // router.push('/home')
}
// console.log(route.path)
// if (route.path === '/') {
//   // setTimeout(() => {
//   router.push('/home')
//   // }, 0)
// }
let i = 0
const onIn = computed(() => {
  console.log(route.path)
  // if (route.path === '/') {
  if (i === 0) {
    return true
  } else {
    return false
  }
})
const toHome = () => {
  i++
  router.push('/home')
}
provide('musicPlayerRef', musicPlayerRef)
</script>
<template>
  <!-- :exclude="['WYYMusic', 'KWMusic']" -->
  <!-- light -->
  <!-- <van-config-provider theme="dark"> -->
  <van-config-provider :theme="theme">
    <router-view v-slot="{ Component }">
      <keep-alive :include="['SearchPage', 'SearchList', 'SetUp']">
        <component :is="Component" />
      </keep-alive>
    </router-view>
  </van-config-provider>
  <!-- 进入主页面 -->
  <!-- </van-button> -->
  <MusicPlayer ref="musicPlayerRef"></MusicPlayer>
  <van-floating-bubble
    @click="onIn ? toHome() : showMusicPlayer()"
    axis="xy"
    :icon="onIn ? 'wap-home-o' : 'audio'"
    magnetic="x"
    :gap="10"
  />
  <!-- <van-floating-bubble
    @click="showMusicPlayer()"
    axis="xy"
    :icon="'audio'"
    magnetic="x"
    :gap="10"
  /> -->
  <!-- <van-floating-bubble
    @click="router.push('/test')"
    axis="xy"
    icon="warn-o"
    magnetic="x"
    :gap="10"
  /> -->
</template>

<style scoped>
/* ::v-deep .inter {
  width: 100%;
} */
</style>
